package t4tools.lepath.util;

import java.util.HashMap;
import java.util.Map;

import org.objectweb.asm.Opcodes;

import t4tools.lepath.generate.Generator;

public class LeClassLoader extends ClassLoader implements Opcodes {

	private Map<String, Class<?>> classes = new HashMap<String, Class<?>>();
	
	public LeClassLoader() {
		super(LeClassLoader.class.getClassLoader());
	}

	@Override
	public Class<?> findClass(String className) {
		Class<?> result = null;
		result = classes.get(className);
		if (result != null) {
			return result;
		}
		try {
			Class<?> target = Class.forName(className.substring(0, className.indexOf("_LePath")));
			byte[] bytes = Generator.generateFor(target);
			result = defineClass(className, bytes, 0, bytes.length, null);
			classes.put(className, result);
			return result;
		} catch (Exception e) {
			return null;
		}
	}
}